package com.sunday.factory.singletonDemo;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @ClassName DoubleCheckSingleton
 * @Author JiangHai
 * @Date 2021/9/5 14:39
 **/
public class DoubleCheckSingleton {

    private AtomicLong id = new AtomicLong(0);

    private static DoubleCheckSingleton instance;

    private DoubleCheckSingleton() {
        // 禁止通过反射创建对象
        throw new IllegalArgumentException();
    }

    public static DoubleCheckSingleton getInstance() {
        if (instance == null) {
            synchronized (DoubleCheckSingleton.class) {
                if (instance == null) {
                    return new DoubleCheckSingleton();
                }
            }
        }
        return instance;
    }

    public long getId() {
        return id.incrementAndGet();
    }
}
